% ------------------------------ Phase shift ---------------------------- %
%   Created Date    : 05 Mar 2013
%   Modified Date   : 28 Oct 2013
%   Version         : 3.1  
%   Author Name     : Sungjin Kim
%   Description     : adjusting zeroth and first phase on time domain with
%                      center offset.
%   Arguements      : input fid data. bandwidth, 1st, 2nd phase (in radian), and offset
% ---------------------------------------------------------------------- %%

%% BEGIN OF FUNCTION %%
function [cmplx_phased_freq, cmplx_phased_fid] = phaseShift(arr_cmplxFidData, bandwidth, zero_order, first_order, center_offsetFreq)

    % obtain data information.
    len_data = length(arr_cmplxFidData);
    shift = floor(len_data/2);
    
    % preparation.
    arr_cmplxFreqData = fft(arr_cmplxFidData);
    arr_cmplxFreqData = circshift(arr_cmplxFreqData, shift);
    magnitude_data = abs(arr_cmplxFreqData);                    % magnitude 
    phase_angle = angle(arr_cmplxFreqData);                     % phase angle
    first_order_shift_array = zeros(len_data, 1);                % make empty array 

    % first-order phase angle %
    for idx_fre = 1 : len_data
        % first order offset from -bandwidth/2 to bandwidth/2
        first_order_shift_array(idx_fre, 1) = first_order * ((idx_fre - len_data/2-1)/len_data*bandwidth - center_offsetFreq);
    end

    phase_angle = phase_angle + zero_order + first_order_shift_array;                    % shifted phase angle.
    real_data = magnitude_data .* cos(phase_angle);                                       % phase shift about real number.
    imag_data = magnitude_data .* sin(phase_angle);                                       % phase shift about imaginary number.
    cmplx_phased_freq = complex(real_data, imag_data);                                    % make complex number.

    % shift the data whether right or left %
    tmp_cmplx_freDomain = circshift(cmplx_phased_freq, -shift); 
    cmplx_phased_fid = ifft(tmp_cmplx_freDomain);                                         % Inverse FFT for making shifted FID dada. 
    
    return;
end % END OF FUNCTION %